package com.aw.qzblog.service.impl;

import com.aw.qzblog.common.Result;
import com.aw.qzblog.entity.dto.article.CreateArticleRequest;
import com.aw.qzblog.entity.enums.ErrorEnum;
import com.aw.qzblog.entity.vo.user.UserBaseInfo;
import com.aw.qzblog.exception.SysException;
import com.aw.qzblog.utils.CurrentUserUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aw.qzblog.entity.model.Article;
import com.aw.qzblog.service.ArticleService;
import com.aw.qzblog.mapper.ArticleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 34889
 * @description 针对表【article】的数据库操作Service实现
 * @createDate 2024-06-22 14:28:11
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
        implements ArticleService {

    /**
     * 用户创建文章
     *
     * @param createArticleRequest 创建参数
     * @return 响应结果
     */
    @Override
    public Result pushArticle(CreateArticleRequest createArticleRequest) {
        if (createArticleRequest == null) {
            throw new SysException(ErrorEnum.INPUT_ERROR);
        }
        if (createArticleRequest.getContent().isEmpty() ||
                createArticleRequest.getTitle().isEmpty() ||
                createArticleRequest.getStatus() < 0) {
            throw new SysException(ErrorEnum.INPUT_ERROR, "用户输入内容不能为空，请检查用户输入");
        }
        Article article = new Article();
        BeanUtils.copyProperties(article, createArticleRequest);
        UserBaseInfo currentUser = CurrentUserUtil.getCurrentUser();
        article.setAuthorId(currentUser.getId());
        this.save(article);
        return Result.success();
    }
}




